package spring.my.db.sharding.demo.response;

import spring.my.db.sharding.demo.exception.BzException;

public class CommonResponse<T> extends Response {

    private T entity;

    public CommonResponse() {
        super();
    }

    public CommonResponse(int status, String message) {
        super(status, message);
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public static <T> CommonResponse<T> getInstance() {
        return new CommonResponse<>();
    }

    public static <T> CommonResponse<T> getInstance(BzException ex) {
        if (ex != null) {
            return getInstance(ex.getCode(), ex.getMessage());
        }
        return getInstance();
    }

    public static <T> CommonResponse<T> getInstance(int code, String mesage) {
        CommonResponse<T> commonResponse = new CommonResponse<T>();
        commonResponse.setStatus(code);
        commonResponse.setMessage(mesage);
        return commonResponse;
    }


    public static <T> CommonResponse<T> getInstance(T t) {
        CommonResponse<T> commonResponse = new CommonResponse<T>();
        commonResponse.setStatus(ResponseCode.SUCCESS.getCode());
        commonResponse.setEntity(t);
        return commonResponse;
    }

    public CommonResponse status(int code) {
        this.setStatus(code);
        return this;
    }

    public CommonResponse message(String message) {
        this.setMessage(message);
        return this;
    }

    public CommonResponse<T> entity(T t) {
        this.setEntity(t);
        return this;
    }

}
